package oa.hleast.xswl.controller.app;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.enums.QueryModeEnum;
import oa.hleast.common.result.Result;
import oa.hleast.common.result.ResultCode;
import oa.hleast.common.web.util.RequestUtils;
import oa.hleast.xswl.common.annotation.UserAuth;
import oa.hleast.xswl.pojo.domain.CmsVenueMain;
import oa.hleast.xswl.pojo.dto.UmsUserDto;
import oa.hleast.xswl.pojo.form.CmsVenueSignInForm;
import oa.hleast.xswl.pojo.vo.CmsVenueScheduleVo;
import oa.hleast.xswl.pojo.vo.HttpShowVenueSignInVo;
import oa.hleast.xswl.pojo.vo.MyVenueReserveVo;
import oa.hleast.xswl.pojo.vo.ShowVenueScheduleVo;
import oa.hleast.xswl.service.IUmsUserService;
import oa.hleast.xswl.service.venue.CmsVenueMainService;
import oa.hleast.xswl.service.venue.VenueService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "【移动端】场馆预约")
@RestController("appVenueController")
@RequestMapping("/api.app/v1/venue")
@Slf4j
@AllArgsConstructor
public class VenueController {

    private final CmsVenueMainService cmsVenueMainService;
    private final VenueService venueService;
    private final IUmsUserService umsUserService;


    @ApiOperation(value = "场馆列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式", paramType = "query", dataType = "QueryModeEnum",dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "venueName", value = "场馆名称", paramType = "query", dataType = "String",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "departmentId", value = "部门ID", paramType = "query", dataType = "String",dataTypeClass = String.class)
    })
    @GetMapping("main/list")
    public Result list(
            String queryMode,
            Integer page,
            Integer limit,
            String venueName,
            Long departmentId) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            default:
                Page<CmsVenueMain> result = cmsVenueMainService.findVenueVoList(page, limit, venueName, departmentId);
                return Result.success(result.getRecords(), result.getTotal());
        }
    }

    @ApiOperation(value = "场馆详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/mian/{id}")
    public Result detail(@PathVariable Long id) {
        return Result.success(cmsVenueMainService.getById(id));
    }

    /**
     * 按年月查找场馆的场次
     */
    @ApiOperation(value = "按年月查找场馆的场次", httpMethod = "GET")
    @GetMapping("/schedule/findScheduleByYearAndMonth")
    public Result findScheduleByYearAndMonth(@RequestParam Integer year, @RequestParam Integer month, @RequestParam Long venueMainId) {
        List<CmsVenueScheduleVo> venueSchedules = venueService.findScheduleByYearAndMonth(year, month, venueMainId);
        return Result.success(venueSchedules);
    }

    /**
     * 当前天以后30天的场次
     */
    @ApiOperation(value = "当前天以后30天的场次", httpMethod = "GET")
    @ApiImplicitParam(name = "venueMainId", value = "venueMainId", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/schedule/find30DayFromNow")
    public Result findSchedule30DayFromNow(@RequestParam Long venueMainId) {
        List<ShowVenueScheduleVo> venueSchedules = venueService.findSchedule30DayFromNow(venueMainId);
        return Result.success(venueSchedules);
    }

    /**
     * 预约场地
     */
    @UserAuth
    @ApiOperation(value = "预约场地", httpMethod = "POST")
    @PostMapping("/reserve/add")
    public Result venueReserve(@RequestBody CmsVenueSignInForm form) {
        Long userId = RequestUtils.getUserId();
        UmsUserDto userDto = umsUserService.getUserDtoById(userId);
        if (ObjectUtils.isEmpty(userDto) || ObjectUtils.isEmpty(userDto.getId())) {
            return Result.failed(ResultCode.USER_NOT_EXIST);
        }
        Integer authStatus = userDto.getAuthStatus();
        // TODO 实名认证
        if (0 == authStatus) {
            return Result.failed(ResultCode.USER_NOT_PHONE);
        } else if (1 == authStatus) {
            return Result.failed(ResultCode.USER_VERIFIED);
        }

        boolean result = venueService.venueReserve(userId, form);

        return Result.success(result);
    }

    /**
     * 我预约的场地
     */
    @UserAuth
    @ApiOperation(value = "我预约的场地", httpMethod = "GET")
    @GetMapping("/reserve/findMyReserve")
    public Result findMyReserve() {
        Long userId = RequestUtils.getUserId();
        UmsUserDto userDto = umsUserService.getUserDtoById(userId);
        if (ObjectUtils.isEmpty(userDto) || ObjectUtils.isEmpty(userDto.getId())) {
            return Result.failed(ResultCode.USER_NOT_EXIST);
        }
        List<MyVenueReserveVo> dataList = venueService.findMyReserve(userId);
        return Result.success(dataList);
    }

    /**
     * 场馆签到
     */
    @UserAuth
    @ApiOperation(value = "场馆签到", httpMethod = "GET")
    @GetMapping("/reserve/signIn")
    public Result venueSignIn(@RequestParam Long signInId) {
        Long userId = RequestUtils.getUserId();
        UmsUserDto userDto = umsUserService.getUserDtoById(userId);
        if (ObjectUtils.isEmpty(userDto) || ObjectUtils.isEmpty(userDto.getId())) {
            return Result.failed(ResultCode.USER_NOT_EXIST);
        }
        boolean result = venueService.reserveSignIn(signInId);

        return Result.judge(result);
    }


    @ApiOperation(value = "场馆预约展示", httpMethod = "GET")
    @GetMapping("/reserve/httpShowVenueSign")
    public Result httpShowVenueSign(@RequestParam Long signInId) {
        HttpShowVenueSignInVo vo = venueService.httpShowVenueSign(signInId);
        return Result.success(vo);
    }

}

